package com.genius.smsweb.studentmanage;

import com.alibaba.fastjson.JSONObject;
import com.genius.smsbase.Controller.GenericController;
import com.genius.smsbase.Domain.Result;
import com.genius.smsbase.Enum.ResultEnum;
import com.genius.smsbase.Exception.SystemException;
import com.genius.smsbase.util.ResultUtil;
import com.genius.smsdao.studentmanage.domain.Student;
import com.genius.smsservice.coursemanage.service.CourseManager;
import com.genius.smsservice.studentmanage.service.StudentManager;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author Mie
 */
@Api(tags = "学生管理-学生", description = "StudentController")
@RestController
@RequestMapping("student")
public class StudentController extends GenericController<Student,Long, StudentManager> {
    StudentManager studentManager;

    @Resource
    CourseManager courseManager;

    /**
     *
     * @param studentManager
     */
    @Autowired
    public void setStudentManager(StudentManager studentManager) {
        this.studentManager = studentManager;
        this.manager = this.studentManager;
    }

    /**
     * 登录
     * @param student
     * @param httpServletRequest
     * @return Result
     */
    @PostMapping("login")
    public Result login(@RequestBody Student student, HttpServletRequest httpServletRequest){
        if(this.studentManager.findByNumber(student.getNumber()).getPassword().equals(student.getPassword())){
            httpServletRequest.getSession().setAttribute("student",this.studentManager.findByNumber(student.getNumber()));
            httpServletRequest.getSession().setAttribute("number",student.getNumber());
            System.out.println("student已经设置");
            return ResultUtil.success("登录成功");
        }else{
            throw new SystemException(ResultEnum.LOGIN_ERROR);
        }
    }

    /**
     * findbynumber
     * @param number
     * @return Student
     */
    @GetMapping("find/{number}")
    public Student findbynumber(@PathVariable String number){
        Student student = studentManager.findByNumber(number);
        if(Objects.nonNull(student)){
            student.setPassword("");
        }
        return student;
    }

    /**
     * findbynumber2
     * @param number
     * @return Student
     */
    @GetMapping("find")
    public Student findbynumber2(String number){
        return findbynumber(number);
    }

    /**
     * updatepswd修改密码
     * @param o
     * @param httpServletRequest
     * @return Result
     */
    @RequestMapping(value = "/fixpswd",method = RequestMethod.POST)
    public Result updatepswd(@RequestBody Object o,HttpServletRequest httpServletRequest){
        String jsontext = JSONObject.toJSONString(o);
        JSONObject jsonObject = JSONObject.parseObject(jsontext);
        String number = (String) jsonObject.get("number");
        String o_pswd = (String) jsonObject.get("o_pswd");
        String n_pswd = (String) jsonObject.get("n_pswd");
        if(Objects.isNull(number)){
            return ResultUtil.error("您的登录信息无效，请重新登录！");
        }
        Student student1 = studentManager.findByNumber(number);
        if(!o_pswd.equals(student1.getPassword())){
            return ResultUtil.error("您输入的旧密码错误");
        }
        student1.setPassword(n_pswd);
        studentManager.save(student1);
        return ResultUtil.success("信息保存成功");
    }

    /**
     * 退出登录
     * @param httpServletRequest
     * @return Result
     */
    @RequestMapping(value = "exit",method = RequestMethod.POST)
    public Result exit(HttpServletRequest httpServletRequest){
        httpServletRequest.getSession().setAttribute("student",null);
        return ResultUtil.success("退出登录成功！");
    }
}
